package com.wwwarehouse.resource_center.fragment.information;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.GoodsAttributesBean;
import com.wwwarehouse.resource_center.bean.goods.GoodsSelectLabelBean;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;

/**
 * @author Joseph on 2017/9/26.
 *         <p/>
 *         信息卡商品属性
 */

public class InfomationShowAttributesListChildFragment extends TheParentFragment {

    ListView lvlist;
    InfomationListAdaper listAdaper ;
    int num = 10;    //每一页的个数
    int pos;        //当前页数
    int count;      //总共几页
    int mItemHeight; //每一个条目的高度
    ArrayList<GoodsAttributesBean.ListBean> list  = new ArrayList<>();

    @Override
    protected int getContentId() {
        return R.layout.fragment_infomation_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        lvlist = findView(view, R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        ArrayList<GoodsAttributesBean.ListBean> lists = b.getParcelableArrayList("data");
        pos = b.getInt("pos");
        num = b.getInt("num");
        for (int i = pos * num; i < num + pos * num; i++) {
            if (i >= 0 && i < lists.size()) {
                list.add(lists.get(i));
            }
        }
        listAdaper = new InfomationListAdaper();
        lvlist.setAdapter(listAdaper);
    }

    private class InfomationListAdaper extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_infomation_attribute, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        LogUtils.showInfoLog("num:"+num);
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        mItemHeight = lvlist.getHeight() / num;
                        layoutParams.height = mItemHeight;
                        LogUtils.showInfoLog("mItemHeight:"+mItemHeight);
                        finalConvertView.setLayoutParams(layoutParams);
                        LogUtils.showInfoLog("getHeight:"+lvlist.getHeight()+"mItemHeight:"+mItemHeight);
                    }
                });
                viewHodler.mName = (TextView) convertView.findViewById(R.id.tv_title);
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            viewHodler.mName.setText(list.get(position).getAttributeName()+": "+list.get(position).getAttributeValue());
            return convertView;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class ViewHodler {
        TextView mName;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {

    }

    @Override
    public void onReLoad(int code) {

    }
}
